<?php

namespace app\api\model;

use app\admin\model\VipCard;
use think\cache\driver\Redis;
use think\Model;


class SpecialCateModel extends ApiModel
{

    protected $expire_time = 7*24*60*60;

    // 表名
    protected $name = 'special_cate';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];



    public function getAccountCate(){
        $list = self::where(['status' => 1])->field('id,name,image')->order('sort desc,id desc')->limit(8)->select();
        foreach($list as &$val){
            $val['image'] =  config('app.image_url').$val['image'];
        }
        return $this->api_success('获取成功',$list);
    }

    public function special()
    {
        return $this->belongsTo(SpecialModel::class, 'id', 'account_cate_id');
    }



    public function getSpecial($params){
        $map[] = ['status','=',1];
        $uid =  $params['uid'] ?? 0;
        $vipDiscount = 0;

        $user = (new MemberModel())->where(['id' => $uid])->find();
        if($user && $user['vip_type'] > 0){
            $vipDiscount = (new VipCard())->where(['id' => $user['vip_type']])->value('discount');
        }

        //获取省份
        $area_name = $params['area_name'] ?? '';
        $redis = new Redis(config('cache.redis'));
        $keys = $area_name.'_spec_'.$vipDiscount;
        $list = $redis->get($keys);
        $list = [];
        if(!$list){
            $list = self::where($map)->field('id,name')->select();
            //获取地区价格
            $area_price = (new AreaPrice())->where(['area_name' => $area_name])->column('price','special_id');
            foreach ($list as $key => &$value){
                //获取分类下的业务
                $special= (new SpecialModel())
                    ->where(['account_cate_id' => $value['id'],'status'=>1])
                    ->field('id,name,default_price,unit')
                    ->order('sort desc,id asc')
                    ->select()->toArray();

                foreach ($special as &$child){
                    if(isset($area_price[$child['id']])){
                        $child['default_price'] = $area_price[$child['id']];
                    }
                     $child['vip_price'] = '';
                    if($vipDiscount){
                        $child['vip_price'] = number_format($child['default_price'] * $vipDiscount / 10,2) . "";
                    }
                }
                $value['children'] = $special;
            }
//            $redis->set($keys,$list,$this->expire_time);
        }
        return $this->api_success('获取成功',$list);

    }


    public function getSearchSpecial($params = array()){
        $area_name = $params['area_name'] ?? '';
        $search = $params['search'] ?? '';
        $uid =  $params['uid'] ?? 0;
        $vipDiscount = 0;
        $user = (new MemberModel())->where(['id' => $uid])->find();
        if($user && $user['vip_type'] > 0){
            $vipDiscount = (new VipCard())->where(['id' => $user['vip_type']])->value('discount');
        }
        //获取地区价格
        $area_price = (new AreaPrice())->where(['area_name' => $area_name])->column('price','special_id');

        $map[] = ['status','=',1];
        $map[] = ['name','like','%'.$search.'%'];

        $special= (new SpecialModel())
            ->where($map)
            ->field('id,name,default_price,unit')
            ->order('sort desc,id asc')
            ->paginate(10);
        foreach ($special as &$child){
            if(isset($area_price[$child['id']])){
                $child['default_price'] = $area_price[$child['id']];
            }
            $child['vip_price'] = '';
            if($vipDiscount){
                $child['vip_price'] = number_format($child['default_price'] * $vipDiscount / 10,2) . "";
            }

        }

        return $this->api_success('获取成功',$special);
    }




}
